var page = 0;
var over=false;
var state=0;
var win=1;
var h;
$(function() {
	h=$(window).height();
							$('#more').hide();
							$('#body').css('height', h-30);
							$('#body').pullToRefresh();
							$('#body').on("pull-to-refresh", function() {
								doFresh();
							});
							$('#body').infinite().on("infinite", function() {
								if (over){
									return;
								}
								over=true;
								$('#more').show();	
								page++;
								doAjax(2);		
							});
							tabClick(1);
})

function tabClick(num){
	win=num;
	if(num==1){
		state=10;
	}else if(num==2){
		state=30;
	}else if(num==3){
		state=31;
	}else if(num==4){
		state=40;
	}else if(num==5){
		state=0;
	}
	$('.weui_navbar a').removeClass('weui_bar_item_on');
	$('.weui_navbar a').eq(num-1).addClass('weui_bar_item_on ');
	doFresh();
	
}

//type 1为刷新 2为加载更多
function doAjax(type) {
	$.showLoading();
	$.ajax({
		url : appPath + "/buyer/getBuyer.html",
		dataType : 'html',
		type : 'post',
		data : {
			token : $.cookie("token"),
			page : page,
			state:state
		},
		success : function(data) {
			$.hideLoading();
			if(type==1){
				$('#list').html(data);
					for(var i=1;i<6;i++){
						if(win==i&&$('#list div').length==0){
							$('#tab'+i).show();
						}else{
							$('#tab'+i).hide();
						}
					}
				
			}else{				
				$(data).appendTo($('#list'));
				$('#more').hide();				
			}
			if($('#over').val()=='true'){					
				over=true;
			}else{
				over=false;
			}
			return;

		},
		error : function() {
			$.hideLoading();
			$('#more').hide();
			$.alert("系统异常，请重试");
			if(type==2&&$('#over').val()!='true'){
				page--;
				over=false;				
			}
		}
	})
}

function doFresh(){
	over=true;
	page=0;
	doAjax(1);
	$('#body').pullToRefreshDone();
	over=false;
}

function gotoBuy(){
	gotoHtml('/index.html');
}

function payOrder(code){
	gotoHtml('/order/payPage.html?code='+code);
}

function doComment(id){
	gotoHtml('/comments/comment.html?goodsOrderId='+id);
}

function cancelOrder(id){
	$.confirm("确定要取消订单吗？", function() {
	$.showLoading();
	$.ajax({
		url : appPath + "/order/cancelOrder.json",
		dataType : 'json',
		type : 'post',
		data : {
			token : $.cookie("token"),
			orderId : id
		},
		success : function(data) {
			$.hideLoading();
			if (data.code == -1) {
				$.hideLoading();
				$.alert("请登录",function(){
					window.location = appPath + "/member/login.html";
				})
				return;
			}
			if (data.code == 0) {
				$.hideLoading();
				$.alert(data.msg);
				return;
			} else{
				$.alert(data.msg);
				$('#o'+id).remove();
			}

		},
		error : function() {
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
	})
}

function cancelPayOrder(id){
	$.confirm("确定要取消订单吗？", function() {
	$.showLoading();
	$.ajax({
		url : appPath + "/order/cancelPayOrder.json",
		dataType : 'json',
		type : 'post',
		data : {
			token : $.cookie("token"),
			orderId : id
		},
		success : function(data) {
			$.hideLoading();
			if (data.code == -1) {
				$.hideLoading();
				$.alert("请登录",function(){
					window.location = appPath + "/member/login.html";
				})
				return;
			}
			if (data.code == 0) {
				$.hideLoading();
				$.alert(data.msg);
				return;
			} else{
				$.alert(data.msg);
				$('#o'+id).remove();
			}

		},
		error : function() {
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
	})
}

function brforeReceipt(id) {
	$('#password').val("");
	$
			.modal({
				title : "支付密码:",
				text : '请输入您的支付密码<br><input id="password" type="password" class="weui_input weui-prompt-input">',
				buttons : [ {
					text : "确定",
					onClick : function() {
						receipt(id);
					}
				}, {
					text : "取消"
				}, {
					text : "设置密码",
					onClick : function() {
						gotoHtml('/setting/editpaypw.html');
					}
				} ]
			});
}

function receipt(id){
	var pass = $('#password').val();
	if (pass.length < 1) {
		$.alert("请输入支付密码");
		return;
	}
	$.showLoading();
	$.ajax({                    
		url : appPath + "/order/receiptStorageOrder.json",
		dataType : 'json',
		type : 'post',
		data : {
			token : $.cookie("token"),
			storageOrderId : id,
			payPass : $.md5(pass + $('#key').val())
		},
		success : function(data) {
			$.hideLoading();
			if (data.code == -1) {
				$.hideLoading();
				$.alert("请登录",function(){
					window.location = appPath + "/member/login.html";
				})
				return;
			}
			if (data.code == 0) {
				$.hideLoading();
				$.alert(data.msg);
				return;
			} else{
				$.alert(data.msg);
				$('#s'+id).remove();
			}

		},
		error : function() {
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
}
